/*
 * Canvas Game Engine - Image Module
 *
 * version: 0.1
 *
 * author: Fabio Rotondo <fabio@rotondo.it>
 *
 */

// Creates a new CGE Image
// Parameters:
//
// 	- ih: 	CGEImageHandler instance
// 	- url:  Image URL
// 	- name: image name
function CGEImage ( ih, url, name )
{
	var self = this;

	this.ih  = ih;
	this.img = document.createElement ( "img" );

	this.img.addEventListener ( "load", function ( e ) 
		{
			ih._loaded += 1;
			console.debug ( "Image: %s loaded", name );
			self.img.removeEventListener ( "load", null, false );

			self.w = self.img.width;
			self.h = self.img.height;

			// Translation points
			self.trans_x = self.w / 2;
			self.trans_y = self.h / 2;
		}, false );

	this.img.src = url;

	this.alpha = 100;
	this.zoom_level = 100;

	this.pattern = null;
	this.rotation = null;
}

CGEImage.prototype.blit = function ( x, y, alpha, dest_w, dest_h )
{
	var ctxt = CGE.canvas.ctxt;
	var _a = CGE.canvas.ctxt.globalAlpha;

	if ( ! dest_w ) dest_w = this.w;
	if ( ! dest_h ) dest_h = this.h;

	if ( alpha === undefined ) alpha = this.alpha;

	if ( alpha < 0 ) alpha = 0;
	if ( alpha > 100 ) alpha = 100;

	this.alpha = alpha;
	this.x  = x;
	this.y  = y;

	if ( this.zoom_level != 100 )
	{
		dest_w = this.w * this.zoom_level;
		dest_h = this.h * this.zoom_level;
	}

	if ( this.rotation === null )
	{
		ctxt.globalAlpha = alpha / 100;
		ctxt.drawImage ( this.img, x, y, dest_w, dest_h );
		ctxt.globalAlpha = _a;
	} else {
		ctxt.save ();

		ctxt.globalAlpha = alpha / 100;
		ctxt.setTransform ( 1, 0, 0, 1, 0, 0 );
		ctxt.translate ( this.x + this.trans_x, this.y + this.trans_y );
		ctxt.rotate ( CGE.angles.get ( this.rotation ) );	
		ctxt.drawImage ( this.img, this.x - this.trans_x, this.y - this.trans_y, dest_w, dest_h );
		ctxt.restore ();
	}
};

// Repeat:
// 	- repeat
// 	- repeat-x
// 	- repeat-y
// 	- no-repeat
CGEImage.prototype.set_pattern = function ( repeat )
{
	if ( ! repeat ) repeat = "repeat";

	this.pattern = CGE.canvas.ctxt.createPattern ( this.img, repeat );
};

CGEImage.prototype.fill = function ( x, y, w, h )
{
	if ( ! this.pattern )
	{
		console.warn ( "No set_pattern() for image: %s", this.name );
		this.set_pattern ();
	}

	CGE.canvas.ctxt.fillStyle = this.pattern;
	CGE.canvas.ctxt.fillRect ( x, y, w, h );
};

CGEImage.prototype.rotate = function ( degrees )
{
	this.rotation = degrees;
};

CGEImage.prototype.zoom = function ( level )
{
	this.zoom_level = level / 100;
};


function CGEImageHandler ()
{
	this._tot = 0;		// Total amount of resources in memory
	this._loaded = 0;		// Total amount of resources loaded

	this.images = {};
}

CGEImageHandler.prototype.add = function ( url, name )
{
	var i;

	if ( ! name ) name = url;

	// You cannot have two images with the same name (identifier)
	if ( this.images [ name ] ) return this.images [ name ];

	this._tot += 1;

	i = new CGEImage ( this, url, name );
	this.images [ name ] = i;

	return i;
};

CGEImageHandler.prototype.get = function ( name )
{
	return this.images [ name ];
};

CGEImageHandler.prototype.load_resources = function ()
{
};

CGEImageHandler.prototype.resources_status = function ()
{
	return { "total" : this._tot, "loaded" : this._loaded };
};

CGEImageHandler.prototype.ready = function ()
{
	return this._tot <= this._loaded;
};

// Register the ImageHandler to the system
CGE.register ( "image_handler", new CGEImageHandler () );
